from typing import Optional

import pytest
from appium import webdriver
from appium.options.ios import XCUITestOptions
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.common.by import By

PHONE = dict(
    platformName="iOS",
    platformVersion="12.1",
    bundleId="com.apple.Preferences",
    deviceName="iPhone 12",
    udid="00008101-0016253C3EE8001E",
    automationName="XCUITest",
    usePrebuiltWDA=True,
    showXcodeLog=True,
    startlWDP=True,
    useXctestrunFile=False
)

SIMULATOR = dict(
    platformName="iOS",
    platformVersion="18.1",
    # bundleId="com.apple.Preferences",
    deviceName="iPhone 16",
    udid="8FD9A7E6-DCD6-4E0A-9C5A-7E381BF5EA5C",
    automationName="XCUITest",
    usePrebuiltWDA=True,
    showXcodeLog=True,
    startlWDP=True,
    useXctestrunFile=False
)


@pytest.fixture(scope='session')
def ios_driver_phone():
    capabilities = SIMULATOR

    appium_server_url = 'http://localhost:4723'
    driver = webdriver.Remote(command_executor=appium_server_url,
                              options=XCUITestOptions().load_capabilities(capabilities))

    yield driver
    driver.quit()


@pytest.fixture(scope="class")
def settings_element_action(request, ios_driver_phone):
    """
    手机设置页面元素操作
    :param request:
    :param ios_driver_phone:
    :return:
    """

    class settings_page_tool:

        def __init__(self):
            self.driver = ios_driver_phone

        def click_general(self):
            """
            点击 通用
            :return:
            """
            self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "通用").click()

        def test123(self):
            pass

    return settings_page_tool()


@pytest.fixture(scope="class")
def click_tools(request, ios_driver_phone):
    """
    点击操作
    :param request:
    :param ios_driver_phone:
    :return:
    """

    class click_tool:

        def __init__(self):
            self.driver = ios_driver_phone

        def click(self, by_value_dict):
            """
            点击 by value的元素
            :return:
            """
            self.driver.find_element(by_value_dict['by'], by_value_dict['value']).click()

    return click_tool()


settings_page_elements = {
    "general": {"by": AppiumBy.ACCESSIBILITY_ID, "value": "通用"}     # 通用按钮
}
